/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import MODEL.Cliente;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MatheuseJessica
 */
public class DaoCliente extends Banco{
    
    public DaoCliente(){
        connection=Conecta();
    }
    
    public List<Cliente> getClientes(){
        
        List<Cliente> cliente = new ArrayList<>();
        try {    
            Statement stm = connection.createStatement();
             // Vamos executar o seguinte comando SQL :  
            String SQL = "SELECT * FROM cliente";  
            ResultSet rs = stm.executeQuery(SQL);  
            
            while(rs.next())  
            {  
               Cliente cli = new Cliente();
                // Os métodos getXXX recuperam os dados de acordo com o tipo SQL do dado:  
               cli.setCodigo(rs.getInt("cod_cliente"));
               cli.setNome(rs.getString("nome"));
               cli.setCpf(rs.getString("cpf"));
               cli.setRg(rs.getString("rg"));
               cli.setRua(rs.getString("rua"));
               cli.setBairro(rs.getString("bairro"));
               cli.setCidade(rs.getString("cidade"));
               cli.setNumero(rs.getString("numero"));
               cli.setTelefone(rs.getString("telefone"));
               cli.setSexo(rs.getString("sexo"));
               cli.setData_registro(rs.getDate("data_registro"));
               cli.setData_nascimento(rs.getDate("data_nascimento"));
               cliente.add(cli);
            } 
            
        } catch (Exception ex) {
            //Logger.getLogger(DaoMusica.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex);
        }finally{
            Fecha();
        }
       
        return cliente;
    }
   
    public void insereCliente(Cliente clie){
        try {
            Date data;
            String SQL = "INSERT INTO cliente VALUES (default,?,?,?,?,?,?,?,?,?,NOW(),NOW())";
            PreparedStatement stmt = connection.prepareStatement(SQL);  
            // preenche os valores  
            stmt.setString(1, clie.getNome());
            stmt.setString(2, clie.getCpf());
            stmt.setString(3, clie.getRg());
            stmt.setString(4, clie.getRua());
            stmt.setString(5, clie.getBairro());
            stmt.setString(6, clie.getCidade());
            stmt.setString(7, clie.getNumero());
            stmt.setString(8, clie.getTelefone());
            stmt.setString(9, clie.getSexo());

            // executa  
            stmt.execute();  
            stmt.close();  
            
        } catch (Exception ex) {
            //Logger.getLogger(DaoMusica.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Insere Cliente: " + ex);
        }finally{
            Fecha();
        }
    }
}
